package com.hsd.netty.client.controller;

import com.hsd.netty.client.cache.OverallStaticCache;
import com.hsd.netty.client.common.NettyMessage;
import com.hsd.netty.client.constants.NettyClientConstants;
import com.hsd.netty.client.context.ContextData;
import com.hsd.netty.client.method.MethodClass;
import com.hsd.netty.client.proxy.ChannelProxy;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author s.he
 * on 2020/6/9
 */
@RestController
public class CalculateController {
    @Autowired
    private ChannelProxy channelProxy;

    @Autowired
    private MethodClass methodClass;



    @GetMapping("/calculate")
    public String calculateNum(Integer a, Integer b) {
        NettyMessage sendMessage = methodClass.getSendMessage(a, b);
        String requestNumber = sendMessage.getNettyHeader().getRequestNumber();
        channelProxy.sendMessage((ChannelHandlerContext)OverallStaticCache.get(NettyClientConstants.CHANNEL_CONTEXT), sendMessage);
        String result = (String) OverallStaticCache.get(requestNumber, 30000L);
        return result;
    }

}
